package org.hzy.test1;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hzy.entity.Dept;

public class CriteriaOrNamed {

	/**
	 * @param args
	 */
	
	static Configuration con=new Configuration().configure();
	static SessionFactory fac=con.buildSessionFactory();
	static Session sess=fac.getCurrentSession();
	
	static void ts1(){
		Transaction tran=sess.beginTransaction();
		Criteria cri=sess.createCriteria(Dept.class);
		cri.add(Restrictions.eq("deptno", 20));//eq是等于，gt是大于，lt是小于,or是或
		List<Dept> list=cri.list();
		System.out.println(list.get(0).getDname());
		tran.commit();
	}
	
	/*static void ts2(){
		Transaction tran=sess.beginTransaction();
		List<Dept> list=sess.createSQLQuery("select {d.*} from Scott.dept {d}").addEntity("d",Dept.class).list();
		
		System.out.println(list.get(0).getDname());
		tran.commit();
		
	}
	
	static void ts3(){
		Transaction tran=sess.beginTransaction();
		String sql="select {d}.dname from scott.dept {d}";
		SQLQuery query=sess.createSQLQuery(sql).addEntity("d",Dept.class);
		List list=query.list();
		System.out.println(list.size());
//		System.out.println(list.get(0).getDname());
		tran.commit();
		
	}*/
	
	static void ts4(){
		Transaction tran=sess.beginTransaction();
		Query quer=sess.getNamedQuery("querydept");
		Dept dept=new Dept();
		dept.setDeptno(10);
		dept.setDname("ACCOUNTING");
		
		quer.setProperties(dept);
		
//		quer.setString("name", "ACCOUNTING");
//		quer.setInteger("deptno", 10);
		
		List result=quer.list();
		Iterator it=result.iterator();
		if(it.hasNext()){
			Dept d=(Dept) it.next();
			System.out.println(d.getDname());
		}
		tran.commit();
	}
	
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		ts1();
//		ts2();
//		ts3();
//		ts4();
		
	}

}
